package com.tedu.day1_25.p1;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class Consumer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory f=new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c=f.newConnection().createChannel();

//        谁先启动谁先定义交换机
        /*
        * @Process
        * 1.定义随机队列
        * 2.定义交换机
        * 3.绑定队列和交换机*/

//        来个随机队列
        String queue= UUID.randomUUID().toString();

        DeliverCallback deliverCallback=new DeliverCallback() {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                String msg=new String(delivery.getBody());
                System.out.println(msg);
            }
        };
        CancelCallback cancelCallback=new CancelCallback() {
            @Override
            public void handle(String s) throws IOException {

            }
        };

//        @Param 1.非持久 2.独占 3.自动删除
        c.queueDeclare(queue, false, true, true, null);
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
//      绑定
        c.queueBind(queue, "logs", "");
        c.basicConsume(queue, true, deliverCallback, cancelCallback);



    }
}
